testsuite: Add a basic icontheme test
authorBenjamin Otte <otte@redhat.com>
Mon, 12 May 2014 16:19:47 +0000 (18:19 +0200)
committerBenjamin Otte <otte@redhat.com>
Wed, 14 May 2014 02:28:35 +0000 (04:28 +0200)
Most of the work here is creating the infrastructure to have a custom
icon theme to add icons to and run tests against.

testsuite/gtk/Makefile.am
testsuite/gtk/icons/16x16/simple.png [new file with mode: 0644]
testsuite/gtk/icons/index.theme [new file with mode: 0644]
testsuite/gtk/icontheme.c [new file with mode: 0644]

index d5a1bcb479cfa31031bd53485c3fa2727da7b3a7..9774a15846e830b6ebbe4bbad1c8f6e16c87b9f7 100644 (file)
@@ -40,6 +40,7 @@ TEST_PROGS +=                         \
        floating                \
        grid                    \
        gtkmenu                 \
+       icontheme               \
        keyhash                 \
        listbox                 \
        no-gtk-init             \
@@ -124,10 +125,15 @@ keyhash_SOURCES   =                                       \
        $(NULL)
 
 
+test_icontheme =                                       \
+       icons/16x16/simple.png                          \
+       icons/index.theme                               \
+       $(NULL)
 
 EXTRA_DIST +=                          \
        file-chooser-test-dir/empty     \
        file-chooser-test-dir/text.txt  \
+       $(test_icontheme)               \
        $(NULL)
 
 GTK_GSETTINGS_SCHEMAS = \
@@ -149,6 +155,7 @@ all-am: gschemas.compiled
 if BUILDOPT_INSTALL_TESTS
 insttestdir = $(pkglibexecdir)/installed-tests
 insttest_PROGRAMS = $(TEST_PROGS)
+insttest_DATA = $(test_icontheme)
 
 %.test: %$(EXEEXT) Makefile
        $(AM_V_GEN) (echo '[Test]' > $@.tmp; \
diff --git a/testsuite/gtk/icons/16x16/simple.png b/testsuite/gtk/icons/16x16/simple.png
new file mode 100644 (file)
index 0000000..91824f9
Binary files /dev/null and b/testsuite/gtk/icons/16x16/simple.png differ
diff --git a/testsuite/gtk/icons/index.theme b/testsuite/gtk/icons/index.theme
new file mode 100644 (file)
index 0000000..79ecb17
--- /dev/null
@@ -0,0 +1,12 @@
+[Icon Theme]
+Name=Icons
+Comment=Testing of the Icon theme code
+Example=16x16/simple.png
+
+Directories=16x16
+
+[16x16]
+Context=16x16 icons
+Size=16
+Type=Fixed
+
diff --git a/testsuite/gtk/icontheme.c b/testsuite/gtk/icontheme.c
new file mode 100644 (file)
index 0000000..2f84616
--- /dev/null
@@ -0,0 +1,86 @@
+#include <gtk/gtk.h>
+
+#include <string.h>
+
+static GtkIconTheme *
+get_test_icontheme (void)
+{
+  static GtkIconTheme *icon_theme = NULL;
+  const char *current_dir;
+
+  if (icon_theme)
+    return icon_theme;
+
+  icon_theme = gtk_icon_theme_new ();
+  gtk_icon_theme_set_custom_theme (icon_theme, "icons");
+  current_dir = g_get_current_dir ();
+  gtk_icon_theme_set_search_path (icon_theme, &current_dir, 1);
+
+  return icon_theme;
+}
+
+static char *
+lookup_flags_to_string (GtkIconLookupFlags flags)
+{
+  GValue flags_value = { 0, }, string_value = { 0, };
+  char *result;
+
+  g_value_init (&flags_value, GTK_TYPE_ICON_LOOKUP_FLAGS);
+  g_value_init (&string_value, G_TYPE_STRING);
+
+  g_value_set_flags (&flags_value, flags);
+  if (!g_value_transform (&flags_value, &string_value))
+    {
+      g_assert_not_reached ();
+    }
+
+  result = g_value_dup_string (&string_value);
+
+  g_value_unset (&flags_value);
+  g_value_unset (&string_value);
+
+  return result;
+}
+
+static void
+assert_icon_lookup (const char         *icon_name,
+                    gint                size,
+                    GtkIconLookupFlags  flags,
+                    const char         *filename)
+{
+  GtkIconInfo *info;
+
+  info = gtk_icon_theme_lookup_icon (get_test_icontheme (), icon_name, size, flags);
+  if (info == NULL)
+    {
+      g_error ("Could not look up an icon for \"%s\" with flags %s at size %d",
+               icon_name, lookup_flags_to_string (flags), size);
+      return;
+    }
+
+  if (!g_str_has_suffix (gtk_icon_info_get_filename (info), filename))
+    {
+      g_error ("Icon for \"%s\" with flags %s at size %d should be \"...%s\" but is \"...%s\"",
+               icon_name, lookup_flags_to_string (flags), size,
+               filename, gtk_icon_info_get_filename (info) + strlen (gtk_icon_info_get_filename (info)) - strlen (filename));
+      return;
+    }
+
+  g_object_unref (info);
+}
+
+static void
+test_basics (void)
+{
+  assert_icon_lookup ("simple", 16, 0, "/icons/16x16/simple.png");
+}
+
+int
+main (int argc, char *argv[])
+{
+  gtk_test_init (&argc, &argv);
+
+  g_test_add_func ("/icontheme/basics", test_basics);
+
+  return g_test_run();
+}